import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;

public class MyStack {
    public int[] elem;
    public int usedSize;
    public MyStack(){
        this.elem=new int[10];
    }

    public void push(int val){
        if (isFull()){
            this.elem= Arrays.copyOf(elem,2*elem.length);     //拷贝elem,并且把它扩容到原来的两倍
        }
        elem[usedSize++]=val;
    }
    public boolean isFull(){
        return usedSize==elem.length;
    }
    public int pop(){
        if (isEmpty()){
            throw new EmptyStackException();
        }
        int val=elem[usedSize-1];
        usedSize--;
        return val;
    }

    public int peek(){
        if (isEmpty()){
            throw new EmptyStackException();
        }
        int val=elem[usedSize-1];
        return val;
    }
    public boolean isEmpty(){
        return usedSize==0;
    }

    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch=s.charAt(i);
            if (ch=='('||ch=='[' ||ch=='{' ){
                stack.push(ch);
            }else{
                if (stack.isEmpty()){
                    return false;
                }
                //判断匹不匹配
                char ch2=stack.peek();
                if ((ch==')'&&ch2=='(') ||(ch==']'&&ch2=='[')||(ch=='}'&&ch2=='{')){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        if(!stack.isEmpty()){
            return false;
        }
        return true;
    }
    public int evalRPN(String[] tokens) {
        Stack<Integer>stack=new Stack<>();
        for (String str:tokens) {
            if (!isOperator(str)){
                int x=Integer.parseInt(str);  //将字符串转变成int形式
                stack.push(x);
            }else{
                int val2=stack.pop();
                int val1=stack.pop();
                switch (str) {
                    case"+":
                        stack.push(val1+val2);
                        break;
                    case"-":
                        stack.push(val1-val2);
                        break;
                    case"*":
                        stack.push(val1*val2);
                        break;
                    case"/":
                        stack.push(val1/val2);
                        break;
                }
            }
        }
        return stack.pop();

    }
    public boolean isOperator(String ch){
        if (ch.equals("+") ||ch.equals("-")||ch.equals("*")||ch.equals("/")){
            return true;
        }else return false;
    }
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer>stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while (!stack.empty() && j < popV.length&& stack.peek() == popV[j]  ) {
                stack.pop();
                j++;
            }
        }
        if (stack.isEmpty()){
            return true;
        }else return false;
    }
}
















































